<?php

namespace Admin\Controller;

class AuthSetController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        //查出所有分组
        $Auth_group = D('Auth_group');
        $this->groups = $Auth_group->select();
    }

    /**
     * 用户管理--首页
     */
    public function user_manage()
    {
        $User = D('User');
        $user_manages = $User->relation(true)->select();
        //统计用户数
        $this->user_num = $User->count();
        $this->assign('user_manages', $user_manages);
        $this->display();
    }

    /**
     * 用户管理--新增
     */
    public function add_user()
    {
        if (IS_POST) {
            $User = D("User"); // 实例化User对象
            if (!$User->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($User->getError());
            } else {
                // 验证通过 可以进行其他数据操作
//                $User->token = make_token(); //在模型里定义
                $id = $User->add();
                $group_ids = I('post.group_id');
                foreach ($group_ids as $group_id) {
                    $data['uid'] = $id;
                    $data['group_id'] = $group_id;
                    M('Auth_group_access')->add($data);
                }
                $this->success('新增用户成功！！', U('user_manage'));
            }
        } else {
            $this->display();
        }
    }

    /**
     * 用户管理---编辑
     */
    public function edit_user()
    {
        $id = I('get.id');
        $this->user = D('User')->relation(true)->where("id = $id")->find();
        //取出用户所在的组的id
        $this->group_id = pluck($this->user['groups'], 'id');
        if (IS_POST) {
            $User = D("User"); // 实例化User对象
            //传入$_POST和2 指定更新时的操作
            if (!$User->create($_POST, 2)) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($User->getError());
            } else {
                // 验证通过 可以进行其他数据操作
                $result = M('User')->where("username = '$User->username' and id != '$id'")->find();
                if (!$result) {
                    $User->where("id = $id")->save();
                }

                //编辑用户组需要先删除用户原来所在的组，重新添加新的组
                M('Auth_group_access')->where("uid = $id")->delete();
                $group_ids = I('post.group_id');
                foreach ($group_ids as $group_id) {
                    $data['uid'] = $id;
                    $data['group_id'] = $group_id;
                    M('Auth_group_access')->add($data);
                }
                $this->success('编辑用户成功！！', U('user_manage'));
            }
        } else {
            $this->display();
        }
    }

    /**
     * 用户管理----ajax删除
     * 删除需要同时从两个表删除数据，用户表和用户明细表
     */
    public function ajax_delete()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            M('Auth_group_access')->where("uid = '$id'")->delete();
            $result = M('User')->where("id = '$id'")->delete();
            if ($result) {
                $info = array('status' => 1, 'msg' => '删除用户信息成功！！');
            } else {
                $info = array('status' => 0, 'msg' => '你没有权限进行此操作，请与管理员联系');
            }
            $this->ajaxReturn($info);
        }
    }

    /**
     * 用户管理---改变状态
     */
    public function change_status()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $status = !I('post.status');
            M('User')->where("id = $id")->setField('status', $status);
        }
    }

    /**
     * 用户组管理---首页
     */
    public function group_manage()
    {
        $Auth_group = M('Auth_group');
        $count = $Auth_group->count();// 查询满足要求的总记录数
        $Page = new \Common\Util\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $pages = $Page->show();// 分页显示输出
        $auth_groups = $Auth_group->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(compact('auth_groups','pages','count x  m'));
        $this->display();
    }


    /**
     * 用户组管理---新增
     */
    public function add_group()
    {
        $Auth_group = M('Auth_group');
        if (IS_AJAX) {
            $data['title'] = trim(I("post.title"));
            $data['rules'] = I("post.rules");

            $info = array('status' => 1, 'msg' => '新增用户组成功');

            if ($data['title'] == "") {
                $info = array('status' => 0, 'msg' => '用户组名不能为空');
            }
            if ($data['rules'] == "") {
                $info = array('status' => 0, 'msg' => '权限不能为空');
            }
            //用户组名称不能重复
            $title = $Auth_group->where("title = '$data[title]'")->find();
            if (isset($title)) {
                $info = array('status' => 0, 'msg' => '该用户组名已存在');
            }

            if ($info['status'] == 1) {
                $data['rules'] = implode(', ', $data['rules']);
                $data['created_at'] = time();
                $Auth_group->add($data);
            }
            $this->ajaxReturn($info);
        } else {
            $this->display();
        }
    }

    /**
     * 用户组管理---编辑
     */
    public function edit_group()
    {
        $id = I('get.id');
        $this->group = M('Auth_group')->find($id);

//        if (IS_POST) {
//            $data['title'] = trim(I('post.title'));
//            $data['rules'] = implode(', ', I('post.rules'));
//            $data['created_at'] = time();
//
//            if($data['title'] == ""){
//                $this->error('用户组名不能为空',U('edit_group'));
//            }
//            M("Auth_group")->where("id = $id")->save($data);
//            $this->success('编辑用户组成功！！', U('group_manage'));
//        } else {
//            $this->display();
//        }

        //ajax方式提交
        if (IS_AJAX) {
            $id = I('post.id');
            $data['title'] = trim(I('post.title'));
            $data['rules'] = I('post.rules');

            $info = array('status' => 1, 'msg' => '编辑用户组成功');
            if ($data['title'] == "") {
                $info = array('status' => 0, 'msg' => '用户组名不能为空');
            }
            if ($data['rules'] == "") {
                $info = array('status' => 0, 'msg' => '权限不能为空');
            }
            $title = M('Auth_group')->where("title = '$data[title]' and id != '$id'")->find();
            if ($title) {
                $info = array('status' => 0, 'msg' => '该用户组名已存在');
            }

            if ($info['status'] == 1) {
                $data['rules'] = implode(', ', $data['rules']);
                $data['created_at'] = time();
                M('Auth_group')->where("id = $id")->save($data);
            }
            $this->ajaxReturn($info);
        } else {
            $this->display();
        }

    }

    /**
     * 用户组管理----删除
     */
    public function delete_group()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            M('Auth_group')->where("id = $id")->delete();
        }
    }


    /**
     * 菜单与权限---首页
     */
    public function rule_manage()
    {
        $this->display();
    }

    /***
     * 菜单与权限---新增
     */
    public function add_rule()
    {
        if (IS_AJAX) {
            $data['parent_id'] = I('post.parent_id');
            $data['icon'] = I("post.icon");
            $data['title'] = I('post.title');
            $data['name'] = I('post.name');

            $face = array('status' => 1, 'msg' => '菜单权限添加成功');
            if ($data['title'] == "") {
                $face = array('status' => 0, 'msg' => '菜单名称不能为空');
            }
            if ($data['name'] == "") {
                $face = array('status' => 0, 'msg' => '权限名称不能为空');
            }

            $title = M('Auth_rule')->where("title = '$data[title]'")->find();
            $name = M('Auth_rule')->where("name = '$data[name]'")->find();
            if ($title) {
                $face = array('status' => 0, 'msg' => '菜单名称已经存在');
            }
            if ($name) {
                $face = array('status' => 0, 'msg' => '权限名称已存在');
            }

            if ($face['status'] == 1) {
                M('Auth_rule')->add($data);
            }
            $this->ajaxReturn($face);
        }
    }

    /**
     * 菜单与权限---编辑
     */
    public function edit_rule()
    {
        $id = I('get.id');
        $this->rule = M("Auth_rule")->where("id = '$id'")->find();
        if (IS_AJAX) {
            $id = I('post.id');
            $data['parent_id'] = I('post.parent_id');
            $data['icon'] = I('post.icon');
            $data['title'] = I('post.title');
            $data['name'] = I('post.name');

            $info = array('status' => 1, 'msg' => '编辑菜单权限成功');
            if ($data['title'] == "") {
                $info = array('status' => 0, 'msg' => '菜单名称不能为空');
            }
            if ($data['name'] == "") {
                $info = array('status' => 0, 'msg' => '权限名称不能为空');
            }
            $title = M('Auth_rule')->where("title = '$data[title]'")->find();
            $name = M('Auth_rule')->where("name = '$data[name]'")->find();

            if ($title) {
                $info = array('status' => 0, 'msg' => '菜单名称已存在');
            }
            if ($name) {
                $info = array('status' => 0, 'msg' => '权限名称已经存在');
            }

            if ($info['status'] == 1) {
                M("Auth_rule")->where("id = '$id'")->save($data);
            }
            $this->ajaxReturn($info);
        } else {
            $this->display();
        }
    }

    /**
     * 菜单与权限---删除
     */
    public function delete_rule()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $count = M('Auth_rule')->where("parent_id = '$id'")->find();
            if ($count) {
                $info = array('status' => 0, 'msg' => '此分类下有二级分类，不能删除');
            } else {
                $info = array('status' => 1, 'msg' => '删除分类成功');
                M("Auth_rule")->where("id = '$id'")->delete();
            }
            $this->ajaxReturn($info);
        }
    }


}